<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveRecord::Integration</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveRecord::Integration 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activerecord/lib/active_record/integration_rb.html">activerecord/lib/active_record/integration.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Integration.html#method-i-cache_key">cache_key</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>T</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Integration.html#method-i-to_param">to_param</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-cache_key">
            
              <b>cache_key</b>()
            
            <a href="Integration.html#method-i-cache_key" name="method-i-cache_key" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a cache key that can be used to identify this record.</p>

<pre class="ruby"><span class="ruby-constant">Product</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">cache_key</span>     <span class="ruby-comment"># =&gt; &quot;products/new&quot;</span>
<span class="ruby-constant">Product</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">5</span>).<span class="ruby-identifier">cache_key</span> <span class="ruby-comment"># =&gt; &quot;products/5&quot; (updated_at not available)</span>
<span class="ruby-constant">Person</span>.<span class="ruby-identifier">find</span>(<span class="ruby-value">5</span>).<span class="ruby-identifier">cache_key</span>  <span class="ruby-comment"># =&gt; &quot;people/5-20071224150000&quot; (updated_at available)</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-cache_key_source')" id="l_method-i-cache_key_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/04cda1848cb847c2bdad0bfc12160dc8d5547775/activerecord/lib/active_record/integration.rb#L48" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-cache_key_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/integration.rb, line 48</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">cache_key</span>
  <span class="ruby-keyword">case</span>
  <span class="ruby-keyword">when</span> <span class="ruby-identifier">new_record?</span>
    <span class="ruby-node">&quot;#{self.class.model_name.cache_key}/new&quot;</span>
  <span class="ruby-keyword">when</span> <span class="ruby-identifier">timestamp</span> = <span class="ruby-identifier">max_updated_column_timestamp</span>
    <span class="ruby-identifier">timestamp</span> = <span class="ruby-identifier">timestamp</span>.<span class="ruby-identifier">utc</span>.<span class="ruby-identifier">to_s</span>(<span class="ruby-identifier">cache_timestamp_format</span>)
    <span class="ruby-node">&quot;#{self.class.model_name.cache_key}/#{id}-#{timestamp}&quot;</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-node">&quot;#{self.class.model_name.cache_key}/#{id}&quot;</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-to_param">
            
              <b>to_param</b>()
            
            <a href="Integration.html#method-i-to_param" name="method-i-to_param" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a <a href="../String.html">String</a>, which Action Pack uses for
constructing an URL to this object. The default implementation returns this
record’s id as a <a href="../String.html">String</a>, or nil if this
record’s unsaved.</p>

<p>For example, suppose that you have a User model, and that you have a
<code>resources :users</code> route. Normally, <code>user_path</code> will
construct a path with the user object’s ‘id’ in it:</p>

<pre class="ruby"><span class="ruby-identifier">user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find_by</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Phusion'</span>)
<span class="ruby-identifier">user_path</span>(<span class="ruby-identifier">user</span>)  <span class="ruby-comment"># =&gt; &quot;/users/1&quot;</span>
</pre>

<p>You can override <code>to_param</code> in your model to make
<code>user_path</code> construct a path using the user’s name instead of
the user’s id:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">User</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier">to_param</span>  <span class="ruby-comment"># overridden</span>
    <span class="ruby-identifier">name</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find_by</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Phusion'</span>)
<span class="ruby-identifier">user_path</span>(<span class="ruby-identifier">user</span>)  <span class="ruby-comment"># =&gt; &quot;/users/Phusion&quot;</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-to_param_source')" id="l_method-i-to_param_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/04cda1848cb847c2bdad0bfc12160dc8d5547775/activerecord/lib/active_record/integration.rb#L38" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-to_param_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/integration.rb, line 38</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">to_param</span>
  <span class="ruby-comment"># We can't use alias_method here, because method 'id' optimizes itself on the fly.</span>
  <span class="ruby-identifier">id</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">id</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-comment"># Be sure to stringify the id for routes</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    